/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bookstore.dao;

import bookstore.idao.IOrderDao;
import bookstore.idao.IOrderDetailsDao;
import bookstore.iservice.IOrderDetailsService;
import bookstore.model.Book;
import bookstore.model.Category;
import bookstore.model.Customer;
import bookstore.model.Order;
import bookstore.model.Orderdetails;
import bookstore.service.OrderDetailsService;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author MY
 */
public class OrderDao implements IOrderDao {

    @Override
    public List<Order> findByPage(Integer pageSize, Integer pageNo) {
        return null;
    }

    @Override
    public List<Order> findOrder(Customer customer) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Order order = null;
        IOrderDetailsService orderDetailsService = new OrderDetailsService();
        Orderdetails orderdetail = null;
        List<Order> orderList = new ArrayList<Order>();
        try {
            con = MySQLHelper.connect();
           
            String sql = "select * from bs_order where cust_id = " + customer.getId() + " order by ord_datetime desc";
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                order = new Order();
               List<Orderdetails> detailsList = new  ArrayList<Orderdetails>();
                order.setCustomer(customer);
                order.setId(rs.getInt("ord_id"));
                order.setDatetime(rs.getTimestamp("ord_datetime"));
                order.setTotal(rs.getFloat("ord_total"));
                order.setOrderState(rs.getString("ord_orderstate"));
                order.setSendWay(rs.getString("ord_sendWay"));
                order.setReciever(rs.getString("ord_reciever"));
                order.setPhone(rs.getString("ord_phone"));
                order.setEmail(rs.getString("ord_email"));
                order.setAddress(rs.getString("ord_address"));
                order.setZipcode(rs.getString("ord_zipcode"));
                order.setAddress(rs.getString("ord_address"));
                System.out.println("asdasdasdasd"+order.getAddress());
                orderDetailsService.setDetailDao(new OrderDetailsDao());
                detailsList=orderDetailsService.findDetails(order);
                order.setOrderdetails(detailsList);
                orderList.add(order);
                System.out.println("asdasdasdasd"+order.getAddress());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MySQLHelper.closePreparedStatement(ps);
            MySQLHelper.closeConneciton(con);
        }
        return orderList;
    }

    ;
    
     @Override
    public int findAllCount() {
        return 0;
    }

    @Override
    public List<Order> findAll() {

        return null;
    }

    @Override
    public Order findById(Integer orderId) {
        return null;
    }

    @Override
    public void delete(Integer orderId) {

    }

    @Override
    public void updatePassword(Order detail) {

    }

    @Override
    public void updateMessage(Order detail) {

    }

    @Override
    public void update(Order detail) {

    }

    public int add(Order order) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int ord_id = 0;
        try {
            con = MySQLHelper.connect();
            String sql = "insert into bs_order(cust_id,ord_total,ord_orderState,ord_sendWay,ord_reciever,ord_phone,ord_email,ord_address,ord_zipcode) values(?,?,?,?,?,?,?,?,?)";
            ps = con.prepareStatement(sql);
            ps.setInt(1, order.getCustomer().getId());
            ps.setFloat(2, order.getTotal());
            ps.setString(3, order.getOrderState());
            ps.setString(4, order.getSendWay());
            ps.setString(5, order.getReciever());
            ps.setString(6, order.getPhone());
            ps.setString(7, order.getEmail());
            ps.setString(8, order.getAddress());
            ps.setString(9, order.getZipcode());
            ps.executeUpdate();
            sql = "select max(ord_id) ord_id from bs_order";
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {

                ord_id = rs.getInt("ord_id");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MySQLHelper.closePreparedStatement(ps);
            MySQLHelper.closeConneciton(con);
        }
        return ord_id;
    }

    @Override
    public void insert(Order obj) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
